var gulp = require('gulp');
var jshint=require('gulp-jshint');
var concat=require('gulp-concat');
var uglify=require('gulp-uglify');
var rename=require('gulp-rename');
var imagemin=require('gulp-imagemin');
var html=require('gulp-minify-html');
var css=require('gulp-minify-css');
var sass=require('gulp-sass');
//利用gulp复制文件
gulp.task('default',function(){
	gulp.src('js/*.js')
	.pipe(gulp.dest('script/'))
});

//js语法错误检查
gulp.task('checkjs',function(){
	gulp.src('js/*.js')
	.pipe(jshint())
	.pipe(jshint.reporter('gulp-jshint-html-reporter',{filename:'err.html'}));
});

//js合并
gulp.task('jsconcat',function(){
	gulp.src('js/*.js')
	.pipe(concat('all.js'))
	.pipe(gulp.dest('script/'));
});

//监听合并
gulp.task('watchjs',function(){
	gulp.watch('js/*.js',function(){
		gulp.run('jsconcat');
	});
});

//js代码的合并和压缩
gulp.task('js',function(){
	gulp.src('js/*.js')
	.pipe(concat('jsall.js'))
	.pipe(gulp.dest('script/'))
	.pipe(rename('jsall.min.js'))
	.pipe(uglify())
	.pipe(gulp.dest('script/'));
});

gulp.task('watchjs1',function(){
	gulp.watch('js/*.js',function(){
		gulp.run('js');
	});
});

//图片压缩.png
gulp.task('image',function(){
	gulp.src('img/*.png')
	.pipe(imagemin())
	.pipe(gulp.dest('images/'));
});

//html文件的压缩
gulp.task('html',function(){
	gulp.src('html/*.html')
	.pipe(html())
	.pipe(gulp.dest('htmlmin/'));
});

//css文件的压缩
gulp.task('css',function(){
	gulp.src('css/*.css')
	.pipe(css())
	.pipe(gulp.dest('cssmin/'));
});

//sass文件的编译和监听
gulp.task('sassrun',function(){
	gulp.src('scss/*.scss')
	.pipe(sass())
	.pipe('scssmin/');
});

gulp.task('watchsass',function(){
	gulp.watch('scss/*.scss',function(){
		gulp.run('sassrun');
	});
});
